/* global axios */
import ApiClient from '../ApiClient'

class ConversationApi extends ApiClient {
  constructor () {
    super('conversations', { accountScoped: true })
  }

  get ({
    inboxId,
    status,
    assigneeType,
    page,
    labels,
    teamId,
    conversationType,
  }) {
    return axios.get(this.url, {
      params: {
        inboxId: inboxId,
        teamId: teamId,
        status,
        assigneeType: assigneeType,
        page,
        labels,
        conversationType: conversationType,
      },
    })
  }

  filter (payload) {
    return axios.post(`${this.url}/filter`, payload.queryData, {
      params: {
        page: payload.page,
      },
    })
  }

  search ({ q }) {
    return axios.get(`${this.url}/search`, {
      params: {
        q,
        page: 1,
      },
    })
  }

  toggleStatus ({ conversationId, status, snoozedUntil = null }) {
    return axios.post(`${this.url}/${conversationId}/toggleStatus`, {
      status,
      snoozedUntil: snoozedUntil,
    })
  }

  assignAgent ({ conversationId, agentId }) {
    return axios.post(
      `${this.url}/${conversationId}/assignedAgent?assigneeId=${agentId}`,
      {}
    )
  }

  assignTeam ({ conversationId, teamId }) {
    const params = { teamId: teamId }
    return axios.post(`${this.url}/${conversationId}/assignedTeam`, params)
  }

  markMessageRead ({ id }) {
    return axios.post(`${this.url}/${id}/updateLastSeen`)
  }

  markMessagesUnread ({ id }) {
    return axios.post(`${this.url}/${id}/unread`)
  }

  toggleTyping ({ conversationId, status, isPrivate }) {
    return axios.post(`${this.url}/${conversationId}/toggleTypingStatus`, {
      typingStatus: status,
      isPrivate: isPrivate,
    })
  }

  mute (conversationId) {
    return axios.post(`${this.url}/${conversationId}/mute`)
  }

  unmute (conversationId) {
    return axios.post(`${this.url}/${conversationId}/unmute`)
  }

  meta ({ inboxId, status, assigneeType, labels, teamId, conversationType }) {
    return axios.get(`${this.url}/meta`, {
      params: {
        inboxId: inboxId,
        status,
        assigneeType: assigneeType,
        labels,
        teamId: teamId,
        conversationType: conversationType,
      },
    })
  }

  sendEmailTranscript ({ conversationId, email }) {
    return axios.post(`${this.url}/${conversationId}/transcript`, { email })
  }

  updateCustomAttributes ({ conversationId, customAttributes }) {
    return axios.post(`${this.url}/${conversationId}/customAttributes`, {
      customAttributes: customAttributes,
    })
  }

  fetchParticipants (conversationId) {
    return axios.get(`${this.url}/${conversationId}/participants`)
  }

  updateParticipants ({ conversationId, userIds }) {
    return axios.patch(`${this.url}/${conversationId}/participants`, {
      userIds: userIds,
    })
  }
}

export default new ConversationApi()
